/**
 * 
 */
package gui.control;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

/**
 * The three buttons control panel for running programs.
 * 
 * Update History:
 * Version 0.1a: The gui is created.  However, there is no formal support for actually running a program.
 * 
 * @author Visual IDE
 * (Nathan Ong)
 * @version 0.1a
 */
public class ThreeButtonGUI
{
	/*
	 * The state controls which buttons are on the panel.
	 * STOP: go, stop(disabled), step
	 * RUN:  go(disabled), stop, pause
	 * As of now, there is no support for PAUSE
	 */
	private enum State{STOP,RUN};		//The states that the control can be in.  As of now, there is no support for a PAUSE
	
	private final JFrame mainFrame;		//The main window for the control panel
	private final JPanel panel;			//The panel to hold the buttons
	private final JButton[] buttons;	//Holds three buttons: go, stop, step/pause

	private State state;				//The current state the control is in
	
	/**
	 * Constructs the control panel.
	 */
	public ThreeButtonGUI()
	{
		//initialization
		this.mainFrame = new JFrame("Control Panel");
		this.panel = new JPanel(new GridLayout(1,3,4,0));
		this.buttons = new JButton[3];
		this.state = State.STOP;
		
		//button creation
		JButton go = new JButton("GO");
		go.addActionListener(new GoButtonListener());
		go.setEnabled(true);
		go.setVisible(true);
		JButton stop = new JButton("STOP");
		stop.addActionListener(new StopButtonListener());
		stop.setEnabled(false);
		stop.setVisible(true);
		JButton pauseStep = new JButton("  STEP  ");//The spaces are added in order to support the PAUSE text
		pauseStep.addActionListener(new PauseStepButtonListener());
		pauseStep.setActionCommand("STEP");
		pauseStep.setEnabled(true);
		pauseStep.setVisible(true);
		
		//add the buttons to the buttons array
		this.buttons[0] = go;
		this.buttons[1] = stop;
		this.buttons[2] = pauseStep;
		
		//add the buttons to the panel
		this.panel.add(go);
		this.panel.add(stop);
		this.panel.add(pauseStep);
		
		//set up the frame
		this.mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.mainFrame.setLayout(new BorderLayout());
		this.mainFrame.add(this.panel,BorderLayout.CENTER);
		this.mainFrame.validate();
		this.mainFrame.pack();
		this.mainFrame.setVisible(true);
	}
	
	/*
	 * Changes the state of the control panel.
	 * @param state The new State of the control.
	 * @return Returns whether or not the state change was successful.
	 */
	private boolean changeState(State state)
	{
		//if the state is the same, there's no point in trying to change it.
		if(this.state == state)
		{
			return false;
		}
		
		//change to which state?
		//For the buttons that should be enabled/disabled, see the comment of the declaration of the enum State.
		switch(state)
		{
			case STOP:
				this.buttons[0].setEnabled(true);
				this.buttons[1].setEnabled(false);
				this.buttons[2].setText("STEP");
				this.buttons[2].setActionCommand("STEP");
				break;
			case RUN:
				this.buttons[0].setEnabled(false);
				this.buttons[1].setEnabled(true);
				this.buttons[2].setText("PAUSE");
				this.buttons[2].setActionCommand("PAUSE");
				break;
			default:
				throw new IllegalStateException("The state " + state.toString() + " is an illegal state.");
		}	
		this.mainFrame.validate();
		this.state = state;
		return true;
	}
	
	/**
	 * The go button listener for the ThreeButtonGUI.
	 * 
	 * Update History:
	 * Version 0.1a: The listener only changes the state to RUN
	 * 
	 * @author Visual IDE
	 * (Nathan Ong)
	 * @version 0.1a
	 */
	public class GoButtonListener implements ActionListener
	{
		public GoButtonListener()
		{
		}

		public void actionPerformed(ActionEvent e)
		{
			System.out.println("GO!");
			changeState(State.RUN);
		}
	}
	
	/**
	 * The pause and step button listener for the ThreeButtonGUI.
	 * 
	 * Update History:
	 * Version 0.1a: If the button is PAUSE, then the state is changed to STOP
	 * 
	 * @author Visual IDE
	 * (Nathan Ong)
	 * @version 0.1a
	 */
	public class PauseStepButtonListener implements ActionListener
	{
		public PauseStepButtonListener()
		{
		}
		
		public void actionPerformed(ActionEvent e)
		{
			String actionCommand = e.getActionCommand();
			System.out.println(actionCommand + "!");
			if(actionCommand.equals("PAUSE"))
			{
				changeState(State.STOP);
			}
		}
	}
	
	/**
	 * The stop button listener for the ThreeButtonGUI.
	 * 
	 * Update History:
	 * Version 0.1a: The listener only changes the state to STOP
	 * 
	 * @author Visual IDE
	 * (Nathan Ong)
	 * @version 0.1a
	 */
	public class StopButtonListener implements ActionListener
	{
		public StopButtonListener()
		{
		}

		public void actionPerformed(ActionEvent e)
		{
			System.out.println("STOP!");
			changeState(State.STOP);
		}
	}

	/**
	 * This main method should only be used for debugging purposes.
	 * @param args
	 */
	public static void main(String[] args)
	{
		@SuppressWarnings("unused")
		ThreeButtonGUI tbgui = new ThreeButtonGUI();
	}
}
